package com.xqit.ydzhkt.common.utils;

import net.sf.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.HashMap;
import java.util.Map;

public class WxcxUtil {

    //从微信后台拿到APPID和APPSECRET 并封装为常量

    //祁县
    //public static final String APPID = "wxcdc3c9c992e767e4";
    //public static final String APPSECRET = "2a37897a31f4d7c07695f85243812d53";

    //新闻
    //public static final String APPID = "wxf7c841a667ff0580";
    //public static final String APPSECRET = "9205764a7abb0af9d43a828301cd8a03";

    //沧县
    //public static final String APPID = "wx379497aceb5a4697";
    //public static final String APPSECRET = "";

    //本地开发
    //pang
    public static final String APPID = "wxb5635a8781e74f6c";
    public static final String APPSECRET = "1f7963bab9db7cb72f9b1c15f9455f2f";

    //hao
    //public static final String APPID = "wx27e84a2a9ed0139f";
    //public static final String APPSECRET = "c993d7e59d7227878e0b413df463491b";


    public static Map<String, Object> sendGetReq(String url) {
        String requestUrl = url;
        HttpClient client = null;
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(requestUrl);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = client.execute(httpget, responseHandler);
            JSONObject OpenidJSONO = JSONObject.fromObject(response);
            String openid = String.valueOf(OpenidJSONO.get("openid"));
            String session_key = String.valueOf(OpenidJSONO.get("session_key"));
            String unionid = String.valueOf(OpenidJSONO.get("unionid"));
            String errcode = String.valueOf(OpenidJSONO.get("errcode"));
            String errmsg = String.valueOf(OpenidJSONO.get("errmsg"));
            String sessionid = new PnoUtil().getEno();
            result.put("openid", openid);
            result.put("sessionKey", session_key);
            result.put("unionid", unionid);
            result.put("errcode", errcode);
            result.put("errmsg", errmsg);
            result.put("sessionid", sessionid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.getConnectionManager().shutdown();
        }
        return result;
    }
}
